package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.entity.Work;
import com.qfedu.hotelssm.service.intf.WorkService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author truman
 * @date 2022/8/28
 */
@RestController
@RequestMapping("/api/work/")
public class WorkController {
    @Autowired
    private WorkService service;

    @PostMapping("save.do")
    public R save(Work work) {
        return service.save(work);
    }

    @GetMapping("del.do")
    public R delete(int id) {
        return service.delete(id);
    }

    @PostMapping("update.do")
    public R update(Work work) {
        return service.update(work);
    }

    @GetMapping("page.do")
    public PageBean select(String name,int page, int limit) {
        return service.selete(name, page, limit);
    }
    /**
     * 下载
     */
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        List<Work> list = service.download();
        response.setHeader("content-disposition","attachment;filename=work-"+System.currentTimeMillis()+".xlsx");
        EasyExcel.write(response.getOutputStream(),Work.class).sheet("作业表").doWrite(list);
    }
    /**
     * 批量删除
     */
    @PostMapping("deletes.do")
    public R deletes(int[] id) {
        return service.deletes(id);
    }
    /**
     * 批量导入
     */
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
}
